<?php

class Pages extends CI_Controller {

	function __construct()
	{
		parent::__construct();

		$this->load->helper( array('url', 'form'));
		$this->load->library('tank_auth');
		/*
		 *   basic js files, plugins and css
		 *   for pages : about, help, etc.
		 */
		$this->css_basic = array('main'); 
		$this->js_basic = array('user/CC', 'user/U', 'main/functions', 'main/events', 'main/init');
		$this->plugin_basic = array('jquery');
		/*
		 *   engine js files, plugins and css 
		 *   for pages : items, users, etc.
		 *   INCLUDE basic files (see upper)
		 */ 
		$this->css_engine = $this->css_basic;
		$this->js_engine = array_merge($this->js_basic, array('main/angular', 'user/UM', 'user/UMC', 'item/I', 'item/IC', 'map/MM', 'map/MC'));
		$this->plugin_engine = array_merge($this->plugin_basic, array('leaflet','jquery_validation'));
	}

	public function view($page = 'home')
	{
		if ( ! file_exists('application/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			//show_404();
			$page='error404';
		}

		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['uid']	= '';
			$data['username']	= 'visitor';
		}
		else
		{
			$data['uid']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
		}

		$data['page'] = $page; // Standard name
		$data['title'] = 'Adon'; // Capitalize the first letter
		$data['css_files'] = $this->css_basic; // Give the css files to load
		$data['plugin'] = $this->plugin_basic; // Load plugins
		$data['js_files'] = $this->js_basic; // Give the js files to load

		if($page == 'fineuploader')
		{
			array_push($data['plugin'], 'fineuploader');
		}

		$this->load->view('templates/header', $data);
		$this->load->view('pages/'.$page, $data);
		$this->load->view('templates/footer', $data);
	}

	public function view_part($page = 'home')
	{
		if ( ! file_exists('application/views/pages/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			//show_404();
			$page='error404';
		}
		$data['page'] = 'Adon'; // $pageStandard name
		$data['title'] = ucfirst($page); // Capitalize the first letter

		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['uid']	= '';
			$data['username']	= 'visitor';
		}
		else
		{
			$data['uid']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
		}

		$this->load->view('pages/'.$page, $data);
	}

	public function items()
	{

		// log in test
		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['uid']	= '';
			$data['username']	= 'visitor';
		}
		else
		{
			$data['uid']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
		}

		$data['page'] = 'items'; // Standard name
		$data['title'] = 'Adon'; // Capitalize the first letter
		$data['plugin'] = $this->plugin_engine;
		//$data['plugin'] = array('jquery', 'jquery_validation'); // Load plugins
		$data['css_files'] = $this->css_engine; // Give the css files to load
		$data['js_files'] = $this->js_engine; // Give the js files to load

		// specific to the page
		//array_push($data['plugin'], 'leaflet', 'fineuploader', 'fb');

		$this->load->view('templates/header', $data);
		$this->load->view('pages/items', $data);
		$this->load->view('templates/footer', $data);
	}

	// Display one item
	public function item($id = null)
	{
		//cache for n minute the page
		//$this->output->cache(3);

		// prepare to send login data
		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['uid']	= '';
			$data['username']	= 'visitor';
		}
		else
		{
			$data['uid']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
		}

		$data['page'] = 'item'; // Standard name
		$data['title'] = 'Adon'; // Capitalize the first letter
		$data['plugin'] = array('jquery', 'jquery_validation'); // Load plugins
		$data['css_files'] = array('main'); // Give the css files to load
		$data['js_files'] = array(); // Give the js files to load
		$data['id'] = $id; // Item id

		array_push($data['plugin'], 'leaflet', 'fineuploader');
		$data['js_files'] = array_merge($data['js_files'], $this->js_adon);

		$this->load->view('templates/header', $data);
		$this->load->view('pages/item', $data);
		$this->load->view('templates/footer', $data);
	}

	// Display users list
	public function users()
	{

	}

	// Display one user
	public function user($uid = null)
	{
		//cache for n minute the page
		//$this->output->cache(3);

		// prepare to send login data
		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['suid']	= '';
			$data['susername']	= 'visitor';
		}
		else
		{
			$data['suid']	= $this->tank_auth->get_user_id();
			$data['susername']	= $this->tank_auth->get_username();
		}

		$data['uid'] = $uid;
		$data['page'] = 'user'; // Standard name
		$data['title'] = 'Adon'; // Capitalize the first letter
		$data['plugin'] = array('jquery', 'jquery_validation'); // Load plugins
		$data['css_files'] = array('main'); // Give the css files to load
		$data['js_files'] = array(); // Give the js files to load

		array_push($data['plugin'], 'leaflet', 'fineuploader');
		$data['js_files'] = array_merge($data['js_files'], $this->js_adon);

		$this->load->view('templates/header', $data);
		$this->load->view('pages/user', $data);
		$this->load->view('templates/footer', $data);
	}

	// Display test page
	public function test()
	{
		//cache for n minute the page
		//$this->output->cache(3);

		// prepare to send login data
		if (!$this->tank_auth->is_logged_in()) {
			// redirect('/auth/login/');
			$data['uid']	= '';
			$data['username']	= 'visitor';
		}
		else
		{
			$data['uid']	= $this->tank_auth->get_user_id();
			$data['username']	= $this->tank_auth->get_username();
		}

		$data['page'] = 'test'; // Standard name
		$data['title'] = 'Adon'; // Capitalize the first letter
		$data['plugin'] = array('jquery', 'jquery_validation'); // Load plugins
		$data['css_files'] = array('main'); // Give the css files to load
		$data['js_files'] = array('main/angular', 'main/functions', 'main/events'); // Give the js files to load

		$this->load->view('templates/header', $data);
		$this->load->view('pages/test', $data);
		$this->load->view('templates/footer', $data);
	}

}

?>